package com.lzx.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzx.article.entity.Articles;
import com.lzx.article.entity.Comments;
import com.lzx.article.entity.SecondComments;
import com.lzx.article.feign.UserClient;
import com.lzx.article.mapper.ArticlesMapper;
import com.lzx.article.mapper.CommentsMapper;
import com.lzx.article.mapper.SecondCommentsMapper;
import com.lzx.article.service.SecondCommentsService;
import com.lzx.article.vo.NicknameVO;
import com.lzx.article.vo.OssVO;
import com.lzx.common.entity.Inform;
import com.lzx.common.enums.InformType;
import com.lzx.common.utils.R;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lzx
 * @since 2023-01-20
 */
@Service
public class SecondCommentsServiceImpl extends ServiceImpl<SecondCommentsMapper, SecondComments> implements SecondCommentsService {
    @Resource
    private SecondCommentsMapper secondCommentsMapper;

    @Resource
    private CommentsMapper commentsMapper;

    @Resource
    private UserClient userClient;

    @Resource
    private ArticlesMapper articlesMapper;

    //保存二级回复
    @Override
    public R publishSecondComments(SecondComments secondComments) {
        secondComments.setCreateTime(new Date());
        secondCommentsMapper.insert(secondComments);

        //发送通知
        Inform inform = new Inform();
        if (secondComments.getSecondUserId() == -1) {
            Comments comments = commentsMapper.selectById(secondComments.getFirstId());
            inform.setUserId(Math.toIntExact(comments.getUserId()));
        } else {
            inform.setUserId(secondComments.getSecondUserId());
        }
        inform.setSender(Math.toIntExact(secondComments.getSecondTwoUserId()));
        inform.setNickname(secondComments.getSecondTwoNickname());
        inform.setAvatar(secondComments.getSecondTwoAvatar());
        inform.setType(InformType.COMMENTARTICLE.getType());
        Articles articles = articlesMapper.selectById(secondComments.getArticleId());
        inform.setMessage(articles.getTitle());
        userClient.saveInform(inform);

        return R.ok().message("回复成功");
    }

    //根据文章id获取一级和二级评论
    @Override
    public R getFirstAndSecondComments(Long articleId) {
        //获取一级评论
        QueryWrapper<Comments> commentsQueryWrapper = new QueryWrapper<>();
        commentsQueryWrapper.eq("article_id", articleId).orderByDesc("is_top").orderByDesc("like_count");
        List<Comments> firstCommentsList = commentsMapper.selectList(commentsQueryWrapper);

        //获取二级评论的内容
        List<Comments> list = firstCommentsList.stream().peek(firstComments -> {
            QueryWrapper<SecondComments> secondCommentsQueryWrapper = new QueryWrapper<>();
            secondCommentsQueryWrapper.eq("first_id", firstComments.getId());
            List<SecondComments> secondCommentsList = secondCommentsMapper.selectList(secondCommentsQueryWrapper);
            firstComments.setSecondCommentsList(secondCommentsList);
        }).collect(Collectors.toList());

        return R.ok().data("list", list);
    }

    //修改用户头像(文章二级评论)
    @Transactional
    @Override
    public String updateAvatarToSecondComments(OssVO ossVO) {
        QueryWrapper<SecondComments> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("second_user_id", ossVO.getId());
        List<SecondComments> secondCommentsList = secondCommentsMapper.selectList(wrapper1);
        for (SecondComments secondComments : secondCommentsList) {
            secondComments.setSecondOneAvatar(ossVO.getAvatar());
            secondCommentsMapper.updateById(secondComments);
        }
        QueryWrapper<SecondComments> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("second_two_user_id", ossVO.getId());
        List<SecondComments> list = secondCommentsMapper.selectList(wrapper2);
        for (SecondComments secondComments : list) {
            secondComments.setSecondTwoAvatar(ossVO.getAvatar());
            secondCommentsMapper.updateById(secondComments);
        }
        return "修改成功";
    }

    //修改用户昵称(文章二级评论)
    @Transactional
    @Override
    public String updateNicknameToSecondComments(NicknameVO nicknameVO) {
        QueryWrapper<SecondComments> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("second_user_id", nicknameVO.getId());
        List<SecondComments> secondCommentsList = secondCommentsMapper.selectList(wrapper1);
        for (SecondComments secondComments : secondCommentsList) {
            secondComments.setSecondOneNickname(nicknameVO.getNickname());
            secondCommentsMapper.updateById(secondComments);
        }
        QueryWrapper<SecondComments> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("second_two_user_id", nicknameVO.getId());
        List<SecondComments> list = secondCommentsMapper.selectList(wrapper2);
        for (SecondComments secondComments : list) {
            secondComments.setSecondTwoNickname(nicknameVO.getNickname());
            secondCommentsMapper.updateById(secondComments);
        }
        return "修改成功";
    }
}
